package com.yangzc.lion.sys.organization.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yangzc.lion.common.controller.BaseController;
import com.yangzc.lion.common.dao.HQLInfo;
import com.yangzc.lion.common.service.IBaseService;
import com.yangzc.lion.common.tag.Page;
import com.yangzc.lion.sys.organization.model.SysAuthRole;
import com.yangzc.lion.sys.organization.model.SysOrgElement;
import com.yangzc.lion.sys.organization.model.SysOrgPerson;
import com.yangzc.lion.sys.organization.service.ISysAuthRoleService;
import com.yangzc.lion.sys.util.ArrayUtil;
import com.yangzc.lion.sys.util.StringUtil;

/**
 * 角色分配
 * 
 * @author yangzc
 * 
 */
@Controller
@RequestMapping("/sys/organization")
public class SysAuthRoleController extends BaseController<SysAuthRole> {
	@Resource(name = "sysAuthRoleService")
	private ISysAuthRoleService sysAuthRoleService;

	@Override
	protected IBaseService getServiceImp() {
		return sysAuthRoleService;
	}
	
	@Override
	protected void changeFindPageHQLInfo(HttpServletRequest request,
			HQLInfo hqlInfo) {
		hqlInfo.setSelectBlock("sysAuthRole.fdId as fdId,sysAuthRole.fdName as fdName,sysAuthRole.fdAlias as fdAlias,sysAuthRole.fdModulePath as fdModulePath,sysAuthRole.fdDescription as fdDescription");
		super.changeFindPageHQLInfo(request, hqlInfo);
	}
	
	/**
	 * 角色分配列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/sysAuthRole")
	public String sysOrgPerson(HttpServletRequest request) {
		Page<Map<String, Object>> page = super.findObjectPage(request);
		request.setAttribute("page", page);
		return "sys/organization/sysAuthRole";
	}
	
	
	/**
	 * 新增角色分配
	 * @param request
	 * @return
	 */
	@RequestMapping("/addSysAuthRole")
	public String addSysAuthRole(HttpServletRequest request) {
		return "sys/organization/editSysAuthRole";
	}
	
	/**
	 * 跳转到角色分配人员
	 * @param request
	 * @return
	 */
	@RequestMapping("/addSysAuthRolePerson")
	public String addSysAuthRolePerson(HttpServletRequest request,String fdId) {
		SysAuthRole role = (SysAuthRole) getServiceImp().findByPrimaryKey(fdId);
		request.setAttribute("role", role);
		return "sys/organization/editSysAuthRolePerson";
	}
	
	/**
	 *角色分配人员列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/getSysAuthRolePerson")
	public String getSysAuthRolePerson(HttpServletRequest request) {
		HQLInfo hqlInfo = new HQLInfo();
		hqlInfo.setModelName("com.yangzc.lion.sys.organization.model.SysOrgPerson");
		Page<SysAuthRole> page = getServiceImp().findPage(hqlInfo);
		request.setAttribute("page", page);
		return "sys/organization/getSysAuthRolePerson";
	}
	
	/**
	 * 保存角色分配人员
	 * @param request
	 * @return
	 */
	@RequestMapping("/saveSysAuthRolePerson")
	public String saveSysAuthRolePerson(HttpServletRequest request,String fdId,String fdOrgElementsId) {
		if(StringUtil.isNotNull(fdOrgElementsId)){
			SysAuthRole role = (SysAuthRole) getServiceImp().findByPrimaryKey(SysAuthRole.class, fdId, true);
			List<SysOrgElement> personList = new ArrayList<SysOrgElement>();
			String ids[] =  fdOrgElementsId.split(";");
			for(String id : ids){
				SysOrgElement person = (SysOrgPerson)getServiceImp().findByPrimaryKey(SysOrgElement.class, id, true);
				personList.add(person);
			}
			if(!ArrayUtil.isEmpty(personList)){
				role.setFdOrgElements(personList);
			}
			getServiceImp().update(role);
		}
		return "redirect:/sys/organization/sysAuthRole.do";
	}
	
	/**
	 * 编辑角色分配
	 * @param request
	 * @return
	 */
	@RequestMapping("/editSysAuthRole")
	public String editSysAuthRole(HttpServletRequest request,Model model) {
		String fdId = request.getParameter("fdId");
		if (!StringUtil.isNull(fdId)) {
			SysAuthRole role = (SysAuthRole) getServiceImp()
					.findByPrimaryKey(fdId);
			model.addAttribute("method", "edit");
			model.addAttribute("role", role);
		}
		return "sys/organization/editSysAuthRole";
	}
	
	/**
	 * 保存角色分配
	 * @param request
	 * @param modelObj 用户对象
	 * @return
	 */
	@RequestMapping("/saveSysAuthRole")
	public String saveSysOrgPerson(HttpServletRequest request,SysAuthRole modelObj) {
		if(StringUtil.isNotNull(modelObj.getFdId())){
			SysAuthRole role = (SysAuthRole) getServiceImp().findByPrimaryKey(modelObj.getFdId());
			if(StringUtil.isNotNull(modelObj.getFdName())){
				role.setFdName(modelObj.getFdName());
			}
			if(StringUtil.isNotNull(modelObj.getFdAlias())){
				role.setFdAlias(modelObj.getFdAlias());
			}
			if(StringUtil.isNotNull(modelObj.getFdModulePath())){
				role.setFdModulePath(modelObj.getFdModulePath());
			}
			if(StringUtil.isNotNull(modelObj.getFdPath())){
				role.setFdPath(modelObj.getFdPath());
			}
			if(StringUtil.isNotNull(modelObj.getFdDescription())){
				role.setFdDescription(modelObj.getFdDescription());
			}
			getServiceImp().update(role);
		}
		else{
			getServiceImp().add(modelObj);
		}
		return "redirect:/sys/organization/sysAuthRole.do";
	}
	
	/**
	 * 删除角色
	 * @param request
	 * @param modelObj 用户对象
	 * @return
	 */
	@RequestMapping("/delSysAuthRole")
	public String delSysAuthRole(HttpServletRequest request,HttpServletResponse response,String fdId) {
		if(StringUtil.isNotNull(fdId)){
			SysAuthRole element = (SysAuthRole) getServiceImp().findByPrimaryKey(fdId);
			try {
				getServiceImp().delete(element);
				response.setCharacterEncoding("utf-8");
				response.getWriter().write("true");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	

}